package com.globallogic.javatrainings.sergiiivanov.model;

import java.io.Serializable;

/**
 * Stores information about personnel in the University.<br>
 * Personnel could be assigned to rooms
 * 
 * @author Sergey
 * 
 */
public class Personnel extends Person implements Serializable {

    private static final long serialVersionUID = 1L;
    private String responcibilities;
    private MyArray<Room> assignedRooms = new MyArray<Room>();

    /**
     * default constructor
     */
    public Personnel() {
        super();
    }

    /**
     * Quick constructor. Creates new instance with provided data.
     * 
     * @param name
     *            - valid name (not null, not empty, unique)
     * @param mobile
     *            - free string
     * @param email
     *            - free string
     * @throws UniversityWarning
     *             name is not valid
     */
    public Personnel(String name, String mobile, String email) throws UniversityWarning {
        super(name, mobile, email);
    }

    /**
     * Assigns personnel to room
     * 
     * @param r
     *            {@link Room} to assign
     */
    protected void addRoom(Room r) {
        if (assignedRooms.indexOf(r) < 0) {
            assignedRooms.add(r);
        }
    }

    /**
     * Removes assignment to the room. Could be called when another personnel
     * assigned to room.<br>
     * 
     * @param r
     *            {@link Room} from collection
     * 
     */
    protected void removeRoom(Room r) {
        assignedRooms.remove(r);
    }

    /**
     * Returns responsibilities, as it was set by
     * {@link #setResponcibilities(String)}<br>
     * but null is converted to Const.STRING_EMPTY
     * 
     * @return String responsibilities
     */
    public String getResponcibilities() {
        return responcibilities == null ? Const.STRING_EMPTY : responcibilities;
    }

    /**
     * Updates responsibilities as provided
     * 
     * @param newResponcibilities
     *            - free string
     */
    public void setResponcibilities(String newResponcibilities) {
        this.responcibilities = newResponcibilities;
    }

    /**
     * Removes links to personnel from assigned rooms<br>
     * Used before removing personnel
     */
    public void removeLinks() {
        for (Room r : getAssignedRooms())
            if (this.equals(r.getResponsiblePerson())) {
                r.setResponsiblePerson(null);
            }
        assignedRooms.clear();
    }

    /**
     * Returns {@link MyArray#copy()} of assigned rooms
     * 
     * @return {@link MyArray} of {@link Room}
     */
    public MyArray<Room> getAssignedRooms() {
        return assignedRooms.copy();
    }
}
